package com.google.code.hibernate.rest.spring;

import org.hibernate.SessionFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.code.hibernate.rest.RestInterface;

/**
 * 
 * @author wangzijian
 * 
 */
public class SpringRestInterface extends RestInterface {

	private static final long serialVersionUID = 1L;

	public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory";

	private String sessionFactoryBeanName = DEFAULT_SESSION_FACTORY_BEAN_NAME;
	
	@Override
	protected SessionFactory getSessionFactory() {
		return (SessionFactory) applicationContext().getBean(sessionFactoryBeanName);
	}

	private WebApplicationContext applicationContext() {
		return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
	}

	public void setSessionFactoryBeanName(String sessionFactoryBeanName) {
		this.sessionFactoryBeanName = sessionFactoryBeanName;
	}

}
